package com.cmbt.controller;

import com.cmbt.bean.Role;
import com.cmbt.service.impl.RoleServiceImpl;
import com.sun.net.httpserver.HttpServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@WebServlet(name = "addRoleController",urlPatterns = "/addRoleController")
public class addRoleController extends HttpServlet {
    private RoleServiceImpl roleServiceImpl = new RoleServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String type = req.getParameter("type");
        if ("toAdd".equals(type)) {
            req.getRequestDispatcher("addrole.jsp").forward(req, resp);

        } else if ("add".equals(type)) {
            Integer roleId = Integer.valueOf(req.getParameter("roleId"));
            String roleName = req.getParameter("roleName");
            String remark = req.getParameter("remark");
            String[] p = req.getParameterValues("permissions");
//            for (int i = 0; i < permissions.length; i++) {
//                System.out.println(permissions[i]);
//            }
//            String a = "";
//            if (permissions.length > 0) {
//                for (int j = 0; j < permissions.length; j++) {
//                    a = a + permissions[j] + ",";
//                }
//                System.out.print(a);
            String permissions = Arrays.toString(p);
            permissions = permissions.substring(1, permissions.length()-1);
            System.out.println(permissions);

                Role role = new Role();
                role.setRoleId(roleId);
                role.setRoleName(roleName);
                role.setRemark(remark);
//                role.setPermissions(permissions);
//                roleServiceImpl.addrole(role);
                req.getRequestDispatcher("/main.jsp").forward(req, resp);


            }
        }




    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}